package com.mdl.mall.controller;

import com.mdl.api.controller.UserController;
import com.mdl.mall.entity.TCustomerInfo;
import com.mdl.mall.entity.TFujian;
import com.mdl.mall.service.TCustomerInfoService;
import com.mdl.mall.service.TFujianService;
import com.mdl.mall.util.QRCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Desc
 * @Author madengling
 * @Time 2020/10/16 13:36
 */
@Api(tags = "二维码相关接口", description = "提供二维码相关的 Rest API")
@RestController
@RequestMapping("/customer")
public class TCustomerInfoController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private TCustomerInfoService customerInfoService;
    @Autowired
    private TFujianService tFujianService;

    @RequestMapping(value = "/downloadBlob")
    public String downloadBlob(Integer id){
        TFujian tFujian = tFujianService.getFujianById(id);
        BASE64Encoder encoder = new BASE64Encoder();
        String Base64Str = encoder.encode(tFujian.getFujian());
        return Base64Str;
    }

    @RequestMapping(value = "/saveBlob",method = RequestMethod.GET)
    public String saveBlob(Integer id){
        log.info("saveBlob start.");
        int res = 0;
        try {
            byte[] buffer = null;
            File file = new File("G:/小科比/ceshi/张三_YT485642656322_20201209094756.jpg");
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
            TFujian tFujian = new TFujian();
            tFujian.setCustomerId(id);
            tFujian.setFujian(buffer);
            res= tFujianService.insertFujian(tFujian);
            if(res > 0){
                log.info("================================ok=============================");
            }else{
                log.info("================================no=============================");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "path";
    }

    @ApiOperation("查看客户接口")
    @RequestMapping(value = "/getCustomers",method = RequestMethod.GET)
    public List<TCustomerInfo> getCustomers(int page, int rows){
        log.info("getCustomers start.");
        List<TCustomerInfo> customerInfoList = customerInfoService.getTCustomerInfo();
        return customerInfoList.subList(page-1,rows);
    }

    @RequestMapping(value = "/createQrCode",method = RequestMethod.GET)
    public String createQrCode(Integer id){
        log.info("createQrCode start.");
        TCustomerInfo customerInfo = customerInfoService.getTCustomerInfoById(id);
        String fileName = customerInfo.getCustomerName()+"_"+customerInfo.getOddNumber()+"_"+new SimpleDateFormat("YYYYMMddHHmmss").format(new Date());
        String path = QRCodeUtil.createQr(customerInfo.getContext(),fileName);
        log.info(path);
        return path;
    }

    @RequestMapping(value = "/addCustomers",method = RequestMethod.POST)
    public List<TCustomerInfo> addCustomers(TCustomerInfo tCustomerInfo){
        log.info("addCustomers start.");
        if(tCustomerInfo!=null){
            customerInfoService.addTCustomerInfo(tCustomerInfo);
        }
        return customerInfoService.getTCustomerInfo();
    }

}
